package com.xbongbong.pro.statistic.service.result;

import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.enums.IndexTypeEnum;
import com.xbongbong.paas.enums.PaymentSheetTypeEnum;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.Arith;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.pro.constant.CommonConstant;
import com.xbongbong.pro.constant.I18nStringConstant;
import com.xbongbong.pro.domain.entity.ChartEntity;
import com.xbongbong.pro.service.toolbox.statistic.util.ChartResultUtil;
import com.xbongbong.pro.statistic.constant.ChartI18Constant;
import com.xbongbong.pro.statistic.enums.IndicatorRuleEnum;
import com.xbongbong.pro.statistic.enums.NumFormatEnum;
import com.xbongbong.pro.statistic.enums.PanoramaEnum;
import com.xbongbong.pro.statistic.enums.SystemCodeEnum;
import com.xbongbong.pro.statistic.enums.ValueTypeEnum;
import com.xbongbong.pro.statistic.pojo.vo.ContractPerformanceInfoVO;
import com.xbongbong.pro.statistic.pojo.vo.EntityInfoVO;
import com.xbongbong.pro.statistic.result.pojo.ChartFindSheetPojo;
import com.xbongbong.pro.statistic.result.pojo.ChartResultBasePojo;
import com.xbongbong.pro.statistic.result.pojo.ChartResultPkPojo;
import com.xbongbong.pro.statistic.result.pojo.ChartResultPojo;
import com.xbongbong.pro.statistic.result.pojo.ElasticAggsPojo;
import com.xbongbong.pro.statistic.result.pojo.MomYoyTimeListPojo;
import com.xbongbong.pro.statistic.result.pojo.PanoramaPojo;
import com.xbongbong.pro.statistic.result.pojo.TopResultPojo;
import com.xbongbong.pro.statistic.result.pojo.dto.StatisticResultDTO;
import com.xbongbong.pro.statistic.result.pojo.dto.StatisticResultInfoDTO;
import com.xbongbong.pro.statistic.result.pojo.vo.ChartResultDetailVO;
import com.xbongbong.pro.statistic.result.pojo.vo.ChartResultPkDetailVO;
import com.xbongbong.pro.statistic.result.pojo.vo.ResultDoubleListVO;
import com.xbongbong.pro.statistic.through.pojo.dto.ThroughDTO;
import com.xbongbong.saas.domain.entity.ContractPerformanceEntity;
import com.xbongbong.saas.domain.entity.PaymentBalanceEntity;
import com.xbongbong.saas.enums.CompanyConfigEnum;
import com.xbongbong.saas.enums.FundAccountFlowEnum;
import com.xbongbong.saas.enums.business.PaymentEnum;
import com.xbongbong.saas.enums.business.PaymentSheetEnum;
import com.xbongbong.saas.enums.business.PaymentTaskEnum;
import com.xbongbong.saas.enums.dictionary.PaymentStatusEnum;
import com.xbongbong.saas.model.PaymentBalanceModel;
import com.xbongbong.sys.domain.entity.UserEntity;
import com.xbongbong.sys.model.CompanyConfigModel;
import org.apache.commons.collections4.CollectionUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import static org.elasticsearch.index.query.QueryBuilders.termsQuery;

/**
 * 回款相关统计
 * @author zcp
 * @version v1.0
 * @date 2019/2/28 13:36
 * @since v1.0
 */
@Service
public class CrmPaymentResult extends ChartResultParentService implements ChartResultSystem, ChartResultPk, PanoramaResult {
    private static final Logger LOG = LoggerFactory.getLogger(CrmPaymentResult.class);
    @Resource
    private CompanyConfigModel companyConfigModel;
    @Resource
    private PaymentBalanceModel paymentBalanceModel;
    @Resource
    private CrmInvoiceResult crmInvoiceResult;

    @Override
    public Object getTarget(ChartResultPojo chartResultPojo, ChartResultDetailVO chartResultDetailVO) throws XbbException {
        SystemCodeEnum systemCodeEnum = chartResultPojo.getSystemCodeEnum();
        switch (systemCodeEnum) {
            case PAYMENT_ED_CHART:
                return paymentEdChart(chartResultPojo, chartResultDetailVO);
            case PAYMENT_ED_HOME:
                return paymentEdHome(chartResultPojo, chartResultDetailVO);
            case PAYMENT_BAD_CHART:
                return paymentBadChart(chartResultPojo, chartResultDetailVO);
            case RED_SHEET_CHART:
                return redSheetChart(chartResultPojo, chartResultDetailVO);
            case PAYMENT_CUSTOMER_CHART_TOP:
                return paymentCustomerChartTop(chartResultPojo, chartResultDetailVO);
            default:
                break;
        }
        return null;
    }

    @Override
    public Object getValueTarget(ChartResultPojo chartResultPojo) throws XbbException {
        SystemCodeEnum systemCodeEnum = chartResultPojo.getSystemCodeEnum();
        switch (systemCodeEnum) {
            case PAYMENT_ED_BOARD:
            case PAYMENT_ED_HOME:
                return paymentEdHome(chartResultPojo);
            case WRITE_OFF_HOME:
                return paymentWriteOffHome(chartResultPojo);
            case PAYMENT_UN_BOARD:
                return paymentUnBoard(chartResultPojo);
            //应收款（资金管理）
            case PAYMENT_UN_HOME:
                return paymentReceivable(chartResultPojo);
            //回款坏账（资金管理）
            case PAYMENT_BAD_HOME:
                return badSheetHome(chartResultPojo);
            //红冲回款（资金管理）
            case RED_SHEET_HOME:
                return redSheetHome(chartResultPojo);
            case PAYMENT_TASK_BOARD:
            case PAYMENT_TASK_HOME:
//                return paymentTaskValue(chartResultPojo);
                //受限于图表同步机制下放到首页看板设置内（之前是进入首页就触发），只有进入首页看板设置才会同步最新图表，这样的话无法保证图表的实时删除，而新回款计划整个模块已废弃，因此该指标直接用应收款的未收代替
                return paymentReceivable(chartResultPojo);
            default:
                break;
        }
        return null;
    }

    @Override
    public Object getPkTarget(UserEntity userEntity, ChartResultPkPojo chartResultPkPojo, ChartResultPkDetailVO chartResultPkDetailVO) throws XbbException {
        SystemCodeEnum systemCodeEnum = chartResultPkPojo.getSystemCodeEnum();
        switch (systemCodeEnum) {
            //pk、完成情况（员工排行）
            case PK_PAYMENT_ED:
            case PERFORMANCE_PAYMENT_ED:
                return pkPaymentEd(userEntity, chartResultPkPojo, chartResultPkDetailVO);
            default:
                break;
        }
        return null;
    }

    /**
     * 系统pk范围规则指标公共接口
     *
     * @param userEntity            登录员工
     * @param chartEntity
     * @param chartResultPkPojo     pk参数
     * @param chartResultPkDetailVO 塞入pk结果
     * @return java.lang.Object
     * @throws XbbException
     * @author zcp
     * @date 2019/3/18 15:34
     * @version v1.0
     * @since v1.0
     */
    @Override
    public Object getPkRuleTarget(UserEntity userEntity, ChartEntity chartEntity, ChartResultPkPojo chartResultPkPojo, ChartResultPkDetailVO chartResultPkDetailVO) throws XbbException {
        SystemCodeEnum systemCodeEnum = chartResultPkPojo.getSystemCodeEnum();
        switch (systemCodeEnum) {
            //pk、完成情况（员工排行）
            case PK_PAYMENT_ED:
            case PERFORMANCE_PAYMENT_ED:
                return pkRulePaymentEd(userEntity, chartEntity, chartResultPkPojo, chartResultPkDetailVO);
            default:
                break;
        }
        return null;
    }

    @Override
    public Object customer(PanoramaPojo panoramaPojo) throws XbbException {
        PanoramaEnum panoramaEnum = panoramaPojo.getPanoramaEnum();
        Integer isBad;
        Integer isRed;
        switch (panoramaEnum) {
            case CUSTOMER_PAYMENT_ED:
                //已回款：非坏账，不限制红冲
                List<String> notBadCodeList = PaymentSheetTypeEnum.getNotBadCodeList();
                return panoramaCustomerPaymentEd(panoramaPojo, notBadCodeList);
            case CUSTOMER_PAYMENT_RED:
                //红冲：非坏账，红冲
                List<String> redCodeList = PaymentSheetTypeEnum.getRedCodeList();
                return panoramaCustomerPaymentEd(panoramaPojo, redCodeList);
            case CUSTOMER_PAYMENT_BAD:
                //坏账：坏账，不限制红冲
                List<String> badCodeList = PaymentSheetTypeEnum.getBadCodeList();
                return panoramaCustomerPaymentEd(panoramaPojo, badCodeList);
            case CUSTOMER_PREPAYMENT_BALANCE:
                return prepaymentBalance(panoramaPojo);
            case CUSTOMER_INVOICE_AMOUNT:
                Integer isCancel = 0;
                Integer isInvoiceRed = null;
                return crmInvoiceResult.panoramaCustomerInvoiceAmount(panoramaPojo, isCancel, isInvoiceRed);
            case CUSTOMER_BALANCE_RECEIVABLE:
                return panoramaCustomerBlanceReceivable(panoramaPojo);
            default:
                break;
        }
        return null;
    }

    /**
     * 获取应收款余额的相关信息
     *
     * @param panoramaPojo
     * @return
     */
    private Object panoramaCustomerBlanceReceivable(PanoramaPojo panoramaPojo) throws XbbException {
        //得到应收款的查询条件
        BoolQueryBuilder paymentQueryBuilder = chartQueryHelp.panoramaQuery4Payment(panoramaPojo);
        String sumField = PaymentEnum.getEsAttr4Keyword(PaymentEnum.UN_AMOUNT);
        double value = systemChartHelp.getDouble4Total(IndexTypeEnum.IDX_SAAS_PAYMENT, paymentQueryBuilder, sumField);
        return ChartResultUtil.formatValue2Double(false, ValueTypeEnum.VALUE_MONEY, false, value, NumFormatEnum.DEFAULT, false);
    }

    /**
     *
     * 获取计划收款未完成的金额
     *
     * @param panoramaPojo
     * @param isFinish 1:未完成 2:已完成
     * @return
     */
    private Object panoramaCustomerPaymentTask(PanoramaPojo panoramaPojo, Integer isFinish) throws XbbException {
        //得到回款计划的查询条件
        BoolQueryBuilder paymentTaskQueryBuilder = chartQueryHelp.panoramaQuery4PaymentTask(panoramaPojo, isFinish);
        String sumField = PaymentTaskEnum.getEsAttr4Keyword(PaymentTaskEnum.PAYMENT_TASK_NUM);
        double value = systemChartHelp.getDouble4Total(IndexTypeEnum.IDX_SAAS_PAYMENT_TASK, paymentTaskQueryBuilder, sumField);
        return ChartResultUtil.formatValue2Double(false, ValueTypeEnum.VALUE_MONEY, false, value, NumFormatEnum.DEFAULT, false);
    }

    /**
     * 未收款
     * @param chartResultPojo
     * @return java.lang.Object
     * @throws XbbException
     * @author zcp
     * @date 2019/3/3 15:25
     * @since v1.0
     * @version v1.0
     */
    public Object paymentReceivable(ChartResultPojo chartResultPojo) throws XbbException {
        double unAmount = paymentUn(chartResultPojo);
        return ChartResultUtil.formatValue2Double(false, ValueTypeEnum.VALUE_MONEY, false, unAmount, NumFormatEnum.getByCode(chartResultPojo.getNumFormat()), chartResultPojo.getNumFormatFlag());
    }

    /**
     * 已收回款
     * @param chartResultPojo
     * @return java.lang.String 
     * @throws XbbException
     * @author zcp
     * @date 2019/3/28 22:47
     * @since v1.0
     * @version v1.0
     */
    public String paymentEdHome(ChartResultPojo chartResultPojo) throws XbbException {
        //已回款：非坏账，不限制红冲
        List<String> codeList = chartQueryHelp.getCodeListForPaymentSheet(chartResultPojo);
        ElasticAggsPojo elasticAggsPojo = paymentEd2ElasticAggsPojo(chartResultPojo, ValueTypeEnum.VALUE_TYPE, codeList);
        return ChartResultUtil.formatValue2String(false, ValueTypeEnum.VALUE_MONEY, chartResultPojo.isThousandSwitch(), elasticAggsPojo.getTotal(), NumFormatEnum.getByCode(chartResultPojo.getNumFormat()), chartResultPojo.getNumFormatFlag());
    }

    /**
     * 回款核销
     * @param chartResultPojo
     * @return java.lang.String
     * @throws XbbException
     * @author zcp
     * @since v1.0
     * @version v1.0
     */
    public String paymentWriteOffHome(ChartResultPojo chartResultPojo) throws XbbException {
        //回款核销只统计4种：已核销、预收款核销、红冲已核销、红冲预收款核销
        List<String> codeList = PaymentSheetTypeEnum.getWriteOffCodeList();
        ElasticAggsPojo elasticAggsPojo = paymentEd2ElasticAggsPojo(chartResultPojo, ValueTypeEnum.VALUE_TYPE, codeList);
        return ChartResultUtil.formatValue2String(false, ValueTypeEnum.VALUE_MONEY, chartResultPojo.isThousandSwitch(), elasticAggsPojo.getTotal(), NumFormatEnum.getByCode(chartResultPojo.getNumFormat()), chartResultPojo.getNumFormatFlag());
    }

    /**
     * 回款单（资金账户统计用）
     *
     * @param chartResultPojo 获取图表结果参数pojo
     * @return java.lang.String
     * @throws XbbException 异常
     * @author 徐俊杰
     * @date 2020/3/18 15:47
     * @since v1.0
     *
     * @since v2.0 2021/5/31 原先指标叫做回款，现在指标叫做流入，计算规则变成流入 = 原逻辑 + 其他收入单的收入汇总 + 收入类型的调拨单
     */
    double paymentSheet4FundAccount(ChartResultPojo chartResultPojo) throws XbbException {
        /*BoolQueryBuilder boolQueryBuilder = chartQueryHelp.paymentQuery4Account(chartResultPojo);
        //回款金额为求和字段
        String sumField = PaymentSheetEnum.getEsAttr4Keyword(PaymentSheetEnum.AMOUNT);
        //回款时间为时间字段
        String fieldTime = PaymentSheetEnum.getEsAttr4Keyword(PaymentSheetEnum.PAYMENT_TIME);
        ElasticAggsPojo double4Agg = systemChartHelp.getDouble4Agg(chartResultPojo, ValueTypeEnum.VALUE_TYPE, IndexTypeEnum.IDX_SAAS_PAYMENT_SHEET, boolQueryBuilder, fieldTime, sumField);
       */
        //封装查询流入的查询条件
        BoolQueryBuilder boolQueryBuilder = chartQueryHelp.FundAccountInQuery4Account(chartResultPojo);
        //回款金额为求和字段
        String sumField = FundAccountFlowEnum.SHEET_AMOUNT.getAttr();
        //回款时间为时间字段
        String fieldTime = FundAccountFlowEnum.SHEET_TIME.getAttr();
        ElasticAggsPojo double4Agg = systemChartHelp.getDouble4Agg(chartResultPojo, ValueTypeEnum.VALUE_TYPE, IndexTypeEnum.IDX_FUND_ACCOUNT_FLOW, boolQueryBuilder, fieldTime, sumField);
        return Double.valueOf(double4Agg.getTotal());
    }

    /**
     * 应收回款金额（简报看板）
     *
     * @param chartResultPojo 内部存系统指标统计时需要的参数
     * @return java.lang.String
     * @throws XbbException 查询应收回款金额时的异常
     * @author 徐俊杰
     * @date 2019/3/12 20:39
     * @since v1.0
     */
    public String paymentUnBoard(ChartResultPojo chartResultPojo) throws XbbException {
        //ES获取应收回款金额
        double unAmount = paymentUn(chartResultPojo);
        return ChartResultUtil.formatValue2Double(false, ValueTypeEnum.VALUE_MONEY, chartResultPojo.isThousandSwitch(), unAmount, NumFormatEnum.getByCode(chartResultPojo.getNumFormat()), chartResultPojo.getNumFormatFlag());
    }

    /**
     * 计划收款（新回款计划）
     * @param chartResultPojo
     * @return java.lang.String
     * @throws XbbException
     * @author zcp
     * @since v1.0
     * @version v1.0
     */
    public String paymentTaskValue(ChartResultPojo chartResultPojo) throws XbbException {
        String sumField = PaymentTaskEnum.getEsAttr4Keyword(PaymentTaskEnum.PAYMENT_TASK_NUM);
        BoolQueryBuilder boolQueryBuilder = chartQueryHelp.paymentTaskQuery4Un(chartResultPojo);
        double unAmount = systemChartHelp.getDouble4Total(IndexTypeEnum.IDX_SAAS_PAYMENT_TASK, boolQueryBuilder, sumField);
        return ChartResultUtil.formatValue2Double(false, ValueTypeEnum.VALUE_MONEY, chartResultPojo.isThousandSwitch(), unAmount, NumFormatEnum.getByCode(chartResultPojo.getNumFormat()), chartResultPojo.getNumFormatFlag());
    }

    /**
     * 图表中心，回款金额分析
     * @param chartResultPojo
     * @param chartResultDetailVO
     * @return java.lang.Object 
     * @throws XbbException
     * @author zcp
     * @date 2019/2/28 18:46
     * @since v1.0
     * @version v1.0
     */
    public Object paymentEdChart(ChartResultPojo chartResultPojo, ChartResultDetailVO chartResultDetailVO) throws XbbException {
        List<String> legendList = Arrays.asList(I18nMessageUtil.getMessage(ChartI18Constant.PAYMENT_ED_CHART_TITLE).split(","));
        String unit = systemChartHelp.getUnit(I18nMessageUtil.getMessage(CommonConstant.YUAN));
        String unit1 = systemChartHelp.getUnit("%");
        List<String> unitList = Arrays.asList(unit, unit, unit, unit1, unit1);
        //已回款：非坏账，不限制红冲
        List<String> notBadCodeList = PaymentSheetTypeEnum.getNotBadCodeList();
        StatisticResultDTO statisticResultDto = getStatisticResultDto4MomYoy(chartResultPojo, legendList, unitList, notBadCodeList);

        //右侧展示总数
        statisticResultDto.setTotalFlag(true);
        statisticResultDto.setValueTitle(I18nMessageUtil.getMessage(ChartI18Constant.TOTAL_PAYMENT_AMOUNT));
        chartResultDetailVO = systemChartHelp.packageResult(statisticResultDto, chartResultPojo, chartResultDetailVO);
        return chartResultDetailVO;
    }

    /**
     * 已收回款（首页）
     *
     * @param chartResultPojo 入参，包含公司id，员工id、时间和部门筛选信息
     * @param chartResultDetailVO 出参
     * @return java.lang.Object
     * @throws XbbException 查询已收回款时抛出异常
     * @author 徐俊杰
     * @date 2019/3/15 15:39
     * @since v1.0
     */
    public Object paymentEdHome(ChartResultPojo chartResultPojo, ChartResultDetailVO chartResultDetailVO) throws XbbException {
        List<String> notBadCodeList = PaymentSheetTypeEnum.getNotBadCodeList();
        ElasticAggsPojo elasticAggsPojo = paymentEd2ElasticAggsPojo(chartResultPojo, ValueTypeEnum.ARRAY_TYPE, notBadCodeList);
        ThroughDTO throughDTO = new ThroughDTO(chartResultPojo, chartResultPojo.getSystemCodeEnum());
        //是否画入表格内 0否1是
        Integer tableFlag = 1;
        String legend = SystemCodeEnum.PAYMENT_ED_BOARD.getName();
        String unit = I18nMessageUtil.getMessage(CommonConstant.YUAN);
        Boolean totalFlag = true;
        String valueTitle = SystemCodeEnum.PAYMENT_ED_BOARD.getName();
        return systemChartHelp.packageResult4One(elasticAggsPojo, chartResultPojo, chartResultDetailVO, throughDTO, tableFlag, legend, unit, totalFlag, valueTitle);
    }

    /**
     * 图表中心，坏账金额分析（不区分业绩分配）
     * @param chartResultPojo
     * @param chartResultDetailVO
     * @return java.lang.Object 
     * @throws XbbException
     * @author zcp
     * @date 2019/2/28 19:23
     * @since v1.0
     * @version v1.0
     */
    public Object paymentBadChart(ChartResultPojo chartResultPojo, ChartResultDetailVO chartResultDetailVO) throws XbbException {
        //查询条件封装坏账的，isBad=1
        List<String> badCodeList = PaymentSheetTypeEnum.getBadCodeList();
        BoolQueryBuilder boolQueryBuilder = chartQueryHelp.paymentQuery4Belong(chartResultPojo, badCodeList);
        String fieldTime = PaymentSheetEnum.getEsAttr4Keyword(PaymentSheetEnum.PAYMENT_TIME);
        String sumField = PaymentSheetEnum.getEsAttr4Keyword(PaymentSheetEnum.AMOUNT);
        /*
         得到坏账金额
         2019/4/6 不再按照多币种聚合求值 getPayment4Agg(chartResultPojo, ValueTypeEnum.ARRAY_TYPE, IndexTypeEnum.IDX_SAAS_PAYMENT_SHEET, boolQueryBuilder, fieldTime, sumField, currencyRateMap);
        */
        ElasticAggsPojo elasticAggsPojo = systemChartHelp.getDouble4Agg(chartResultPojo, ValueTypeEnum.ARRAY_TYPE, IndexTypeEnum.IDX_SAAS_PAYMENT_SHEET, boolQueryBuilder, fieldTime, sumField);
        //设置值的类型
        chartResultPojo.setValueTypeEnum(ValueTypeEnum.VALUE_MONEY);
        //处理穿透
        ThroughDTO throughDTO = new ThroughDTO(chartResultPojo, chartResultPojo.getSystemCodeEnum());
        //是否画入表格内 0否1是
        Integer tableFlag = 1;
        String legend = PaymentEnum.BAD_AMOUNT.getAttrName();
        String unit = chartResultPojo.getUnit();
        Boolean totalFlag = true;
        String valueTitle = I18nMessageUtil.getMessage(ChartI18Constant.TOTAL_PAYMENT_BAD_AMOUNT);
        return systemChartHelp.packageResult4One(elasticAggsPojo, chartResultPojo, chartResultDetailVO, throughDTO, tableFlag, legend, unit, totalFlag, valueTitle);
    }

    /**
     * 图表中心，红冲退款金额分析
     * @param chartResultPojo
     * @param chartResultDetailVO
     * @return java.lang.Object 
     * @throws XbbException
     * @author zcp
     * @date 2019/2/28 19:23
     * @since v1.0
     * @version v1.0
     */
    public Object redSheetChart(ChartResultPojo chartResultPojo, ChartResultDetailVO chartResultDetailVO) throws XbbException {
        List<String> legendList = Arrays.asList(I18nMessageUtil.getMessage(ChartI18Constant.PAYMENT_RED_SHEET_CHART_TITLE).split(","));
        String unit = systemChartHelp.getUnit(I18nMessageUtil.getMessage(CommonConstant.YUAN));
        String unit1 = systemChartHelp.getUnit("%");
        List<String> unitList = Arrays.asList(unit, unit, unit, unit1, unit1);
        //红冲：非坏账，红冲
        List<String> redCodeList = PaymentSheetTypeEnum.getRedCodeList();
        StatisticResultDTO statisticResultDto = getStatisticResultDto4MomYoy(chartResultPojo, legendList, unitList, redCodeList);

        //右侧展示总数
        statisticResultDto.setTotalFlag(true);
        statisticResultDto.setValueTitle(I18nMessageUtil.getMessage(ChartI18Constant.TOTAL_AMOUNT_RED_PAYMENT_AMOUNT));
        chartResultDetailVO = systemChartHelp.packageResult(statisticResultDto, chartResultPojo, chartResultDetailVO);
        return chartResultDetailVO;
    }

    /**
     * 红冲退款金额（首页）
     *
     * @param chartResultPojo 入参，包含公司id，员工id、时间和部门筛选信息
     * @return java.lang.Object
     * @throws XbbException 查询红冲退款金额时抛出异常
     * @author 徐俊杰
     * @date 2019/3/15 15:54
     * @since v1.0
     */
    public Object redSheetHome(ChartResultPojo chartResultPojo) throws XbbException {
        List<String> redCodeList = PaymentSheetTypeEnum.getRedCodeList();
        ElasticAggsPojo elasticAggsPojo = paymentEd2ElasticAggsPojo(chartResultPojo, ValueTypeEnum.VALUE_TYPE, redCodeList);
        return ChartResultUtil.formatValue2String(false, ValueTypeEnum.VALUE_MONEY, false, elasticAggsPojo.getTotal(), NumFormatEnum.getByCode(chartResultPojo.getNumFormat()), chartResultPojo.getNumFormatFlag());
    }

    /**
     * 回款金额客户排名
     * @param chartResultPojo
     * @param chartResultDetailVO
     * @return java.lang.Object
     * @throws XbbException
     * @author zcp
     * @date 2019/3/1 1:21
     * @since v1.0
     * @version v1.0
     */
    public Object paymentCustomerChartTop(ChartResultPojo chartResultPojo, ChartResultDetailVO chartResultDetailVO) throws XbbException {
        chartResultPojo.setValueTypeEnum(ValueTypeEnum.VALUE_MONEY);
        //已回款：非坏账，不限制红冲
        List<String> notBadCodeList = PaymentSheetTypeEnum.getNotBadCodeList();
        //已回款查询条件
        BoolQueryBuilder boolQueryBuilder = chartQueryHelp.paymentQuery4Belong(chartResultPojo, notBadCodeList);

        String termsField = PaymentSheetEnum.getEsAttr4Keyword(PaymentSheetEnum.CUSTOMER_ID);
        String sumField = PaymentSheetEnum.getEsAttr4Keyword(PaymentSheetEnum.AMOUNT);
        TopResultPojo topResultPojo = systemChartHelp.getTopResultPojo4CustomerTop(IndexTypeEnum.IDX_SAAS_PAYMENT_SHEET, chartResultPojo.getCorpid(), boolQueryBuilder, termsField, sumField);
        String seriesName = I18nMessageUtil.getMessage(I18nStringConstant.PAYMENT_AMOUNT);
        return systemChartHelp.result4Top(chartResultPojo, chartResultDetailVO, topResultPojo, seriesName);
    }
    /**
     * 已回款：该方法获取出总值、和各个桶内数值list，不会封装成图表需要的数据格式
     * @param chartResultPojo
     * @param valueTypeEnum
     * @return com.xbongbong.pro.statistic.pojo.vo.ResultDoubleListVO
     * @throws XbbException
     * @author zcp
     * @date 2019/2/28 19:53
     * @since v1.0
     * @version v1.0
     */
    public ResultDoubleListVO paymentEd2ResultDoubleListVO(ChartResultPojo chartResultPojo, ValueTypeEnum valueTypeEnum) throws XbbException {
        //已回款：非坏账，不限制红冲
        List<String> notBadCodeList = PaymentSheetTypeEnum.getNotBadCodeList();
        ElasticAggsPojo elasticAggsPojo = paymentEd2ElasticAggsPojo(chartResultPojo, valueTypeEnum, notBadCodeList);
        return systemChartHelp.getResultDoubleListVO(elasticAggsPojo);
    }
    /**
     * 得到未收款总金额（未收款只统计总数，无列表等趋势图统计）
     * @param chartResultPojo
     * @return com.xbongbong.pro.statistic.pojo.vo.ResultDoubleListVO
     * @throws XbbException
     * @author zcp
     * @date 2019/3/3 15:21
     * @since v1.0
     * @version v1.0
     */
    public double paymentUn(ChartResultPojo chartResultPojo) throws XbbException {
        String sumField = PaymentEnum.getEsAttr4Keyword(PaymentEnum.UN_AMOUNT);
        BoolQueryBuilder boolQueryBuilder = chartQueryHelp.paymentQuery4Un(chartResultPojo);
        /*
         2019/4/6 不再按照多币种聚合求值 getPaymentUnAmount(chartResultPojo, IndexTypeEnum.IDX_SAAS_PAYMENT, boolQueryBuilder, currencyRateMap);
        */
        return systemChartHelp.getDouble4Total(IndexTypeEnum.IDX_SAAS_PAYMENT, boolQueryBuilder, sumField);
    }

    /**
     * 回款金额pk
     * @param userEntity 登录员工
     * @param chartResultPkPojo pk参数
     * @param chartResultPkDetailVO 塞入pk结果
     * @return com.xbongbong.pro.statistic.pojo.vo.ChartResultPkDetailVO
     * @throws XbbException
     * @author zcp
     * @date 2019/3/20 22:10
     * @since v1.0
     * @version v1.0
     */
    public ChartResultPkDetailVO pkRulePaymentEd(UserEntity userEntity, ChartEntity chartEntity, ChartResultPkPojo chartResultPkPojo, ChartResultPkDetailVO chartResultPkDetailVO) throws XbbException {
        String corpid = chartResultPkPojo.getCorpid();
        chartResultPkPojo.setValueTypeEnum(ValueTypeEnum.VALUE_MONEY);
        //合同业绩分配是否开启
        boolean openRule = companyConfigModel.hasBeenEnable(corpid, CompanyConfigEnum.CONTRACT_PERFORMANCE_RULE_SET.getAlias(),"1");
        List<String> codeList = PaymentSheetTypeEnum.getCodeListForPK();
        if (openRule) {
            //pk榜、不限制分配人
            List<ContractPerformanceInfoVO> performanceInfoVoList = getRuleContractPerformanceInfoVoList(codeList, chartEntity, chartResultPkPojo, true);
            return pkRuleHelp.getPkResult4Performance(performanceInfoVoList, userEntity, chartEntity, chartResultPkPojo, chartResultPkDetailVO);
        }else {
            //已回款：非坏账，不限制红冲
            //塞入基础查询条件
            BoolQueryBuilder boolQueryBuilder = chartRuleQueryHelp.paymentQuery4Base(chartResultPkPojo, codeList);
            String fieldGroup = PaymentSheetEnum.getEsAttr4Keyword(PaymentSheetEnum.BELONG_ID) + ".keyword";

            if (chartDtoHelp.checkJoinRuleIfAll(chartEntity, chartResultPkPojo)) {
                List<String> joinUserIdIn = chartResultPkPojo.getUserIdIn();
                if(CollectionUtils.isNotEmpty(joinUserIdIn)){
                    boolQueryBuilder.filter(termsQuery(fieldGroup, joinUserIdIn));
                }
            }
            int pkType = chartResultPkPojo.getPkType();
            IndexTypeEnum indexTypeEnum = IndexTypeEnum.IDX_SAAS_PAYMENT_SHEET;
            //String fieldGroup = PaymentSheetEnum.getEsAttr4Keyword(PaymentSheetEnum.BELONG_ID) + ".keyword";
            String sumField = PaymentSheetEnum.getEsAttr4Keyword(PaymentSheetEnum.WRITE_OFF_AMOUNT);
            return pkRuleHelp.getPkResult4Double(userEntity, chartEntity, chartResultPkPojo, chartResultPkDetailVO, indexTypeEnum, boolQueryBuilder, fieldGroup, sumField, pkType);
        }
    }

    /**
     * 回款金额pk
     * @param userEntity 登录员工
     * @param chartResultPkPojo pk参数
     * @param chartResultPkDetailVO 塞入pk结果
     * @return com.xbongbong.pro.statistic.pojo.vo.ChartResultPkDetailVO 
     * @throws XbbException
     * @author zcp
     * @date 2019/3/20 22:10
     * @since v1.0
     * @version v1.0
     */
    public ChartResultPkDetailVO pkPaymentEd(UserEntity userEntity, ChartResultPkPojo chartResultPkPojo, ChartResultPkDetailVO chartResultPkDetailVO) throws XbbException {
        String corpid = chartResultPkPojo.getCorpid();
        chartResultPkPojo.setValueTypeEnum(ValueTypeEnum.VALUE_MONEY);
        //合同业绩分配是否开启
        boolean openRule = companyConfigModel.hasBeenEnable(corpid, CompanyConfigEnum.CONTRACT_PERFORMANCE_RULE_SET.getAlias(),"1");
        List<String> codeList = PaymentSheetTypeEnum.getCodeListForPK();
        if (openRule) {
            //pk榜、不限制分配人
            List<ContractPerformanceInfoVO> performanceInfoVoList = getContractPerformanceInfoVoList(codeList, chartResultPkPojo, false);
            return pkHelp.getPkResult4Performance(performanceInfoVoList, userEntity, chartResultPkPojo, chartResultPkDetailVO);
        }else {
            //已回款：非坏账，不限制红冲
            //塞入基础查询条件
            BoolQueryBuilder boolQueryBuilder = chartQueryHelp.paymentQuery4Base(chartResultPkPojo, codeList);
            int pkType = chartResultPkPojo.getPkType();
            IndexTypeEnum indexTypeEnum = IndexTypeEnum.IDX_SAAS_PAYMENT_SHEET;
            String fieldGroup = PaymentSheetEnum.getEsAttr4Keyword(PaymentSheetEnum.BELONG_ID) + ".keyword";
            String sumField = PaymentSheetEnum.getEsAttr4Keyword(PaymentSheetEnum.WRITE_OFF_AMOUNT);
            return pkHelp.getPkResult4Double(userEntity, chartResultPkPojo, chartResultPkDetailVO, indexTypeEnum, boolQueryBuilder, fieldGroup, sumField, pkType);
        }
    }

    /**
     * 客户画像--待收款额
     * @param panoramaPojo
     * @return java.lang.String
     * @author zcp
     * @date 2019/6/3 22:16
     * @since v1.0
     * @version v1.0
     */
    public String panoramaCustomerPaymentUn(PanoramaPojo panoramaPojo) throws XbbException {
        //未收款状态list：未收款、应收款、部分收款
        List<String> statusIn = PaymentStatusEnum.getUnStatusIn();
        //不排除特定阶段，限制负责人，且不限制时间
        BoolQueryBuilder boolQueryBuilder = chartQueryHelp.panoramaQuery4PaymentUn(panoramaPojo, panoramaPojo.getDataId(), statusIn);

        String sumField = PaymentEnum.getEsAttr4Keyword(PaymentEnum.UN_AMOUNT);
        double value = systemChartHelp.getDouble4Total(IndexTypeEnum.IDX_SAAS_PAYMENT, boolQueryBuilder, sumField);
        return ChartResultUtil.formatValue2Double(false, ValueTypeEnum.VALUE_MONEY, false, value, NumFormatEnum.DEFAULT, false);
    }

    /**
     * 全景图--客户的已回款、红冲回款、坏账
     *
     * @param panoramaPojo 全景图参数pojo
     * @param typeList 类型
     * @return java.lang.String
     * @throws XbbException
     * @author zcp
     * @date 2019/6/4 13:52
     * @version v1.0
     * @since v1.0
     */
    public String panoramaCustomerPaymentEd(PanoramaPojo panoramaPojo, List<String> typeList) throws XbbException {
        //得到回款单的查询条件
        BoolQueryBuilder sheetQueryBuilder = chartQueryHelp.panoramaQuery4PaymentEd(panoramaPojo, typeList);
        String sumField = PaymentSheetEnum.getEsAttr4Keyword(PaymentSheetEnum.AMOUNT);
        double value = systemChartHelp.getDouble4Total(IndexTypeEnum.IDX_SAAS_PAYMENT_SHEET, sheetQueryBuilder, sumField);
        return ChartResultUtil.formatValue2Double(false, ValueTypeEnum.VALUE_MONEY, false, value, NumFormatEnum.DEFAULT, false);
    }

    /**
     * 回款坏账（首页资金管理）
     *
     * @param chartResultPojo 图表请求入参
     * @return java.lang.Object
     * @throws XbbException 异常
     * @author 徐俊杰
     * @date 2020/3/11 15:46
     * @since v1.0
     */
    private Object badSheetHome(ChartResultPojo chartResultPojo) throws XbbException {
        List<String> badCodeList = PaymentSheetTypeEnum.getBadCodeList();
        ElasticAggsPojo elasticAggsPojo = paymentEd2ElasticAggsPojo(chartResultPojo, ValueTypeEnum.VALUE_TYPE, badCodeList);
        return ChartResultUtil.formatValue2String(false, ValueTypeEnum.VALUE_MONEY, false, elasticAggsPojo.getTotal(), NumFormatEnum.getByCode(chartResultPojo.getNumFormat()), chartResultPojo.getNumFormatFlag());
    }

    //-------------------private方法-------------------

    /**
     * 获取已回款统计结果
     * @param chartResultPojo 参数pojo
     * @param valueTypeEnum   返回值类型
     * @param typeList 类型
     * @return com.xbongbong.paas.elasticsearch.pojo.ElasticAggsPojo
     * @throws XbbException
     * @author zcp
     * @date 2019/2/28 18:42
     * @version v1.0
     * @since v1.0
     */
    public ElasticAggsPojo paymentEd2ElasticAggsPojo(ChartResultPojo chartResultPojo, ValueTypeEnum valueTypeEnum, List<String> typeList) throws XbbException {
        String corpid = chartResultPojo.getCorpid();
        chartResultPojo.setValueTypeEnum(ValueTypeEnum.VALUE_MONEY);
        Boolean performancePk = chartResultPojo.getPerformancePk();
        IndicatorRuleEnum indicatorRuleEnum = chartResultPojo.getSystemCodeEnum().getIndicatorRuleEnum();
        boolean writeOffFlag = chartDtoHelp.judgePermissionAndIfCheck(chartResultPojo) || performancePk || Objects.equals(indicatorRuleEnum, IndicatorRuleEnum.WRITE_OFF);
        //合同业绩分配是否开启
        boolean openRule = companyConfigModel.hasBeenEnable(corpid, CompanyConfigEnum.CONTRACT_PERFORMANCE_RULE_SET.getAlias(),"1");
        if (openRule) {
            //非pk榜、限制分配人
            List<ContractPerformanceInfoVO> perforList = getContractPerformanceInfoVoList(typeList, chartResultPojo, true);
            //封装最后返回结果
            return systemChartHelp.getResult4OpenPerforRule(chartResultPojo, perforList);
        }else {
            BoolQueryBuilder boolQueryBuilder = chartQueryHelp.paymentQuery4Belong(chartResultPojo, typeList);
            //根据权限决定请求回款金额还是核销金额
            String sumField;
            if (writeOffFlag) {
                sumField = PaymentSheetEnum.getEsAttr4Keyword(PaymentSheetEnum.WRITE_OFF_AMOUNT);
            } else {
                sumField = PaymentSheetEnum.getEsAttr4Keyword(PaymentSheetEnum.AMOUNT);
            }
            String fieldTime = PaymentSheetEnum.getEsAttr4Keyword(PaymentSheetEnum.PAYMENT_TIME);
            /*--------得到回款金额--------*/
            // 2019/4/6 不按照多币种计算
            return systemChartHelp.getDouble4Agg(chartResultPojo, valueTypeEnum, IndexTypeEnum.IDX_SAAS_PAYMENT_SHEET, boolQueryBuilder, fieldTime, sumField);
        }
    }
    /**
     * 获取并封装环比、同比统计结果
     * @param chartResultPojo 参数pojo
     * @param legendList 图的legend
     * @param unitList 各条线的单位，与legendList大小一致
     * @param typeList 类型
     * @return com.xbongbong.pro.statistic.pojo.dto.StatisticResultDTO
     * @throws XbbException
     * @author zcp
     * @date 2019/2/28 18:45
     * @since v1.0
     * @version v1.0
     */
    private StatisticResultDTO getStatisticResultDto4MomYoy(ChartResultPojo chartResultPojo, List<String> legendList, List<String> unitList, List<String> typeList) throws XbbException {
        chartResultPojo.setValueTypeEnum(ValueTypeEnum.VALUE_MONEY);
        //先把chartResultPojo拷贝出一份，下面都用chartResultPojoCopy，防止时间的变更对原有chartResultPojo内参数的影响
        Integer startTime = chartResultPojo.getStartTime();
        Integer endTime = chartResultPojo.getEndTime();
        //环比，上月开始--截止，startTime的前一个月，作为环比的开始时间；endTime的前一个月，作为环比的截止时间
        Integer lastMonthStartTime = DateUtil.getFirstDateOfLastMonth(startTime);
        Integer lastMonthEndTime = DateUtil.getFirstDateOfLastMonth(endTime);
        //同比，上年同期（上年同一个月）开始--截止：startTime的上年同月，作为同比的开始时间；endTime的上年同月第一天，作为同比的截止时间
        Integer lastYearStartTime = DateUtil.getFirstDateOfLastYear(startTime);
        Integer lastYearEndTime = DateUtil.getFirstDateOfLastYear(endTime);

        //------当前筛选时间的结果
        ElasticAggsPojo thisMonthAggsPojo = paymentEd2ElasticAggsPojo(chartResultPojo, ValueTypeEnum.ARRAY_TYPE, typeList);
        ChartResultPojo chartResultPojoCopy = new ChartResultPojo();
        BeanUtil.copyProperties(chartResultPojo, chartResultPojoCopy);
        //------环比结果，把开始结束时间置为环比的开始结束
        chartResultPojoCopy.setStartTime(lastMonthStartTime);
        chartResultPojoCopy.setEndTime(lastMonthEndTime);
        //获取环比同比各自的时间列表
        List<Integer> startTimeList = chartResultPojoCopy.getStartTimeList();
        MomYoyTimeListPojo momYoyTimeListPojo = ChartResultUtil.momYoyTimeListPojo(startTimeList);
        //环比时间处理
        ChartResultUtil.momOrYoyTime(chartResultPojoCopy, true, startTime, endTime);
        chartResultPojoCopy.setStartTimeList(momYoyTimeListPojo.getMomTimeListPojo().getStartTimeList());
        chartResultPojoCopy.setEndTimeList(momYoyTimeListPojo.getMomTimeListPojo().getEndTimeList());
        //------环比结果
        ElasticAggsPojo lastMonthAggsPojo = paymentEd2ElasticAggsPojo(chartResultPojoCopy, ValueTypeEnum.ARRAY_TYPE, typeList);
        //------同比结果，把开始结束时间置为同比的开始结束
        chartResultPojoCopy.setStartTime(lastYearStartTime);
        chartResultPojoCopy.setEndTime(lastYearEndTime);
        //同比时间处理
        ChartResultUtil.momOrYoyTime(chartResultPojoCopy, false, startTime, endTime);
        chartResultPojoCopy.setStartTimeList(momYoyTimeListPojo.getYoyTimeListPojo().getStartTimeList());
        chartResultPojoCopy.setEndTimeList(momYoyTimeListPojo.getYoyTimeListPojo().getEndTimeList());
        //------同比结果
        ElasticAggsPojo lastYearAggsPojo = paymentEd2ElasticAggsPojo(chartResultPojoCopy, ValueTypeEnum.ARRAY_TYPE, typeList);
        //------处理环比、同比比例
        ElasticAggsPojo resultMom = systemChartHelp.getMomYoyResult(thisMonthAggsPojo, lastMonthAggsPojo);
        ElasticAggsPojo resultYoy = systemChartHelp.getMomYoyResult(thisMonthAggsPojo, lastYearAggsPojo);

        List<StatisticResultInfoDTO> statisticResultInfoDtoList = systemChartHelp.getInfoDtos4MomYoy(chartResultPojo, legendList, unitList, thisMonthAggsPojo, lastMonthAggsPojo, lastYearAggsPojo, resultMom, resultYoy);
        StatisticResultDTO statisticResultDto = new StatisticResultDTO(true, thisMonthAggsPojo.getTotal(), null, statisticResultInfoDtoList);
        return statisticResultDto;
    }

    /**
     * pk榜-开启业绩分配
     * （1）先查tb_saas_payment_sheet（限制回款时间范围）
     * （2）然后再查tb_saas_contract_performance表（不限制组织架构）
     * （3）再查tb_saas_contract，基于合同处理多币种，最终得到所有符合条件的数据数组---2019/4/6 不用处理多币种
     * @param typeList 类型
     * @param chartResultBasePojo 统计参数pojo
     * @param userFlag 是否在查询时限制分配人list
     * @return java.util.List<com.xbongbong.pro.statistic.pojo.vo.ContractPerformanceInfoVO>
     * @throws XbbException
     * @author zcp
     * @date 2019/3/21 9:36
     * @since v1.0
     * @version v1.0
     */
    private List<ContractPerformanceInfoVO> getContractPerformanceInfoVoList(List<String> typeList, ChartResultBasePojo chartResultBasePojo, boolean userFlag) throws XbbException {
        //开启业绩分配的回款（已回款、红冲回款）：非坏账
        BoolQueryBuilder sheetQueryBuilder = chartQueryHelp.paymentPerformanceQuery(chartResultBasePojo, typeList);
        //-------------业绩分配：组织架构
        BoolQueryBuilder performanceQueryBuilder = chartQueryHelp.contractQuery4performance(userFlag, chartResultBasePojo);
        //-------------（1）先查回款单，限制回款时间--然后把contractId作为条件塞入performanceQueryBuilder和contractQueryBuilder，并且得到合同id--回款相关信息list（1个合同对应n个回款，每个回款的回款时间、回款金额不一定相同）
        ChartFindSheetPojo chartFindSheetPojo = formHelp.findSheet(sheetQueryBuilder, true);
        //-------------（2）查询tb_saas_contract_performance
        List<ContractPerformanceInfoVO> performanceInfoVoList = findContractPerformance(chartResultBasePojo.getCorpid(), chartFindSheetPojo, performanceQueryBuilder);
        //-------------（3）查询合同，基于合同处理【多币种】---2019/4/6 不用处理多币种
        //perforList = findContract(perforList, contractQueryBuilder, currencyRateMap);
        return performanceInfoVoList;
    }

    /**
     * pk榜-开启业绩分配
     * （1）先查tb_saas_payment_sheet（限制回款时间范围）
     * （2）然后再查tb_saas_contract_performance表（不限制组织架构）
     * （3）再查tb_saas_contract，基于合同处理多币种，最终得到所有符合条件的数据数组---2019/4/6 不用处理多币种
     * @param typeList 类型
     * @param chartResultBasePojo 统计参数pojo
     * @param userFlag 是否在查询时限制分配人list
     * @return java.util.List<com.xbongbong.pro.statistic.pojo.vo.ContractPerformanceInfoVO>
     * @throws XbbException
     * @author zcp
     * @date 2019/3/21 9:36
     * @since v1.0
     * @version v1.0
     */
    private List<ContractPerformanceInfoVO> getRuleContractPerformanceInfoVoList(List<String> typeList, ChartEntity chartEntity, ChartResultBasePojo chartResultBasePojo, boolean userFlag) throws XbbException {
        //开启业绩分配的回款（已回款、红冲回款）：非坏账
        BoolQueryBuilder sheetQueryBuilder = chartQueryHelp.paymentPerformanceQuery(chartResultBasePojo, typeList);
        //-------------业绩分配：组织架构
        BoolQueryBuilder performanceQueryBuilder = chartQueryHelp.contractRuleQuery4performance(userFlag, chartEntity, chartResultBasePojo);
        //-------------（1）先查回款单，限制回款时间--然后把contractId作为条件塞入performanceQueryBuilder和contractQueryBuilder，并且得到合同id--回款相关信息list（1个合同对应n个回款，每个回款的回款时间、回款金额不一定相同）
        ChartFindSheetPojo chartFindSheetPojo = formHelp.findSheet(sheetQueryBuilder, true);
        //-------------（2）查询tb_saas_contract_performance
        List<ContractPerformanceInfoVO> performanceInfoVoList = findContractPerformance(chartResultBasePojo.getCorpid(), chartFindSheetPojo, performanceQueryBuilder);
        return performanceInfoVoList;
    }

    /**
     * 查询业绩分配，得到所占比例
     * @param corpid 公司id
     * @param chartFindSheetPojo 存放回款单关联合同id集合 及 合同id--回款相关信息list（1个合同对应n个回款，每个回款的回款时间、回款金额不一定相同）对应关系map
     * @param perforQueryBuilder 查tb_saas_contract_performance表的查询构造类
     * @return java.util.List<com.xbongbong.pro.statistic.pojo.vo.ContractPerformanceInfoVO> 
     * @throws XbbException
     * @author zcp
     * @date 2019/3/7 14:03
     * @since v1.0
     * @version v1.0
     */
    private List<ContractPerformanceInfoVO> findContractPerformance(String corpid, ChartFindSheetPojo chartFindSheetPojo, BoolQueryBuilder perforQueryBuilder) throws XbbException {
        Map<Long, List<EntityInfoVO>> cidSheetListMap = chartFindSheetPojo.getCidSheetListMap();
        List<ContractPerformanceInfoVO> performanceInfoVoList = new ArrayList<>();
        List<ContractPerformanceEntity> list = formHelp.getContractPerformanceList(corpid, perforQueryBuilder, chartFindSheetPojo);
        for (ContractPerformanceEntity entity : list) {
            String userId = entity.getUserId();
            Long contractId = entity.getContractId();
            //该rate在表内储存时是做了乘以100处理的
            Double rate = entity.getRate();
            //业绩分配是基于contract查询出来，因此，业绩分配内contractId肯定在cidSheetListMap内有对应
            List<EntityInfoVO> sheetList = cidSheetListMap.get(contractId);
            for (EntityInfoVO sheet : sheetList) {
                Double amount = sheet.getAmount();
                Integer time = sheet.getTime();
                if (amount == null) {
                    amount = 0D;
                } else {
                    //处理分配人所占比例
                    amount = Arith.mul(amount, rate);
                    amount = Arith.div(amount, 100, 2);
                }

                ContractPerformanceInfoVO contractPerformanceInfoVO = new ContractPerformanceInfoVO();
                contractPerformanceInfoVO.setContractId(contractId);
                contractPerformanceInfoVO.setUserId(userId);
                contractPerformanceInfoVO.setTime(time);
                contractPerformanceInfoVO.setAmount(amount);
                performanceInfoVoList.add(contractPerformanceInfoVO);
            }
        }
        return performanceInfoVoList;
    }

    /**
     * 获取预付款余额
     *
     * @param panoramaPojo 全景图入参
     * @return java.lang.Object
     * @throws XbbException 异常
     * @author 徐俊杰
     * @date 2019/11/27 14:59
     * @since v1.0
     */
    private Object prepaymentBalance(PanoramaPojo panoramaPojo) throws XbbException {
        Long customerId = panoramaPojo.getDataId();
        String corpid = panoramaPojo.getCorpid();
        PaymentBalanceEntity paymentBalance = paymentBalanceModel.getByCustomerId(customerId, corpid);
        double advancePaymentBalance = paymentBalance == null ? 0 : paymentBalance.getAdvancePaymentBalance();
        return ChartResultUtil.formatValue2Double(false, ValueTypeEnum.VALUE_MONEY, false, advancePaymentBalance, NumFormatEnum.DEFAULT, false);
    }
}
